import { Component, OnInit, Input } from '@angular/core';
import { AlertController, ModalController } from '@ionic/angular';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { EventService } from 'src/app/shared/service/event.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';

@Component({
  selector: 'app-audit-submit-choose',
  templateUrl: './audit-submit-choose.component.html',
  styleUrls: ['./audit-submit-choose.component.scss'],
})
export class AuditSubmitChooseComponent implements OnInit {
  @Input() tagData: any = {};
  checked = false;
  edit = false;
  isOpen = false;
  showAuditSubmitButton = true;
  setCustomBoxVisibility = 'true';
  constructor(
    public appGlobalService: AppGlobalService,
    private http: HttpService,
    public alertController: AlertController,
    public eventService: EventService,
    public modalController: ModalController,
    public pageModalService: PageModalService,

  ) { }

  ngOnInit() {
    this.tagData.data.edit = false;
    this.eventService.eventEmit.on('submitRes', res => {
      const data = JSON.parse(res);
      if (data.res && data.submit) {
        this.success(data.res, data.submit);
      }

    });


    if (this.tagData.data && this.tagData.data
      && this.tagData.data.setCheckBoxVisibility !== undefined
    ) {
      this.setCustomBoxVisibility = this.tagData.data.setCheckBoxVisibility;
    }

    // 2024.01.24 新增参数联调
    if (this.tagData.data && this.tagData.data
      && this.tagData.data.enableSetCustomBoxVisibility !== undefined
      && this.tagData.data.enableSetCustomBoxVisibility === 'true'
    ) {

      this.eventService.eventEmit.on('setCustomBoxVisibility', (obj) => {
        this.setCustomBoxVisibility = obj;
      });

      if (this.appGlobalService.setCustomBoxVisibility !== undefined) {
        this.setCustomBoxVisibility = this.appGlobalService.setCustomBoxVisibility;
      }
    }




    this.eventService.eventEmit.on('recommendAllResult', res => {
      this.checked = res;
    });
    this.eventService.eventEmit.on('chooseAllResult', res => {
      this.checked = res;
    });
    this.eventService.eventEmit.on('manageShopping', res => {
      this.edit = !this.edit;
      if (this.tagData.data.submitButton && this.tagData.data.submitButton.submit && this.tagData.data.submitButton.submit.id) {
        this.appGlobalService.shoppingCarId = this.tagData.data.submitButton.submit.id;
      }
    });

    this.eventService.eventEmit.on('needManageShopping', res => {
      if (this.tagData.data.submitButton && this.tagData.data.submitButton.submit && this.tagData.data.submitButton.submit.id) {
        this.appGlobalService.shoppingCarId = this.tagData.data.submitButton.submit.id;
      }
    });

    this.eventService.eventEmit.on('isOpen', res => {
      this.isOpen = res;
    });
    this.eventService.eventEmit.on('Total', res => {
      this.tagData.data.firstRowRight.label = res.saleTotal;
      if (this.tagData.data.secondRowRight) {
        this.tagData.data.secondRowRight.label = res.oTotal;
      }
    });

    this.eventService.eventEmit.on('hiddenAuditSubmitButton', res => {
      if (res !== undefined && res === '销售中') {
        this.showAuditSubmitButton = false;
      } else {
        this.showAuditSubmitButton = true;
      }
    });

    if (this.eventService.eventEmit.listenerCount('orderValue') > 0) {
      this.eventService.eventEmit.removeListener('orderValue');
    }
    this.eventService.eventEmit.on('orderValue', res => {
      if (res.num1 === 0) {
        if (this.tagData.data && this.tagData.data.firstRowRight) {
          this.tagData.data.firstRowRight.label1 = res.num2;
        }
      } else {
        if (this.tagData.data && this.tagData.data.firstRowRight) {
          this.tagData.data.firstRowRight.label1 = Number(res.num3);
        }
      }
    });

    if (this.eventService.eventEmit.listenerCount('numResults') > 0) {
      this.eventService.eventEmit.removeListener('numResults');
    }
    this.eventService.eventEmit.on('numResults', res => {
      if (this.tagData.data && this.tagData.data.secondRowRight) {
        this.tagData.data.secondRowRight.label1 = res.num1;
      }
      if (this.tagData.data && this.tagData.data.firstRowRight) {
        this.tagData.data.firstRowRight.label1 = res.num2;
      }

    });
    if (this.eventService.eventEmit.listenerCount('deftA') > 0) {
      this.eventService.eventEmit.removeListener('deftA');
    }
    this.eventService.eventEmit.on('deftA', res => {
      this.edit = true;
    });
  }

  allClick() {
    this.checked = !this.checked;
    this.eventService.eventEmit.emit('chooseAll', this.checked);
  }
  passClick(submit, noNeedChoose?) {
    if (noNeedChoose) {
      submit.noNeedChoose = noNeedChoose; // 直接link跳转
    }
    if (submit.params && submit.params.shop_ship_address_id && submit.params.useEventAuditNow === undefined) {
      this.appGlobalService.addressEventResultValue = submit.params.shop_ship_address_id;
      this.appGlobalService.addressEventResultName = 'shop_ship_address_id';
      this.eventService.eventEmit.emit('auditNowForNew', submit);
    } else if (submit.params && submit.params.shop_local_address_id && submit.params.useEventAuditNow === undefined) {
      this.appGlobalService.addressEventResultValue = submit.params.shop_local_address_id;
      this.appGlobalService.addressEventResultName = 'shop_local_address_id';
      this.appGlobalService.shopAddressSelectItemName = '';
      this.appGlobalService.shopAddressSelectItemLocation = '';
      this.appGlobalService.shopAddressSelectItemPhone = '';
      this.eventService.eventEmit.emit('auditNowForNew', submit);
    } else {
      this.appGlobalService.addressEventResultValue = '';
      this.eventService.eventEmit.emit('auditNow', submit);
    }

  }
  noPassClick(link, e?) {
    if (e) {
      e.stopPropagation();
    }

    this.eventService.eventEmit.emit('auditNow', link);
  }
  submitButton2Click(link, e?) {
    if (e) {
      e.stopPropagation();
    }
    this.eventService.eventEmit.emit('auditNow', link);
  }
  success(res, item) {
    if (res.message) {
      this.appGlobalService.showToast(res.message, 2000, 'middle');
    }
    if (item.tabClick) {
      const tabIndex = parseInt(item.tabClick, 10);
      this.eventService.eventEmit.emit("topTabChangeByIndex", tabIndex);
    }
    if (item.back) {
      if (Number(item.back) < 0) {
        const back = Math.abs(Number(item.back));
        for (let index = 0; index < back; index++) {
          this.appGlobalService.popTarget();
        }
        this.appGlobalService.goBack(Number(item.back));
        if (item.submit.content.needRefresh) {
          this.needRefreshTab();
        }
      } else if (Number(item.back) === 0) {
        this.http.reloadCurrentPage();
      } else {
        this.appGlobalService.goBack(Number(item.back));
      }
      localStorage.setItem("needRefresh", "true");
    } else if (res && res.type === 'modal' || item.link && (item.link.type === 'modal' || item.link.type === 'pageModal')) {
      const modalLink = {
        target: res.target,
        params: res.params ? res.params : {},
        sessionId: localStorage.getItem('sessionId'),
        type: ''
      };
      modalLink.type = res.type;
      this.pageModalService.openPage(modalLink, '', res => {
        // 提现
        if (item.submit.content.passwordKey) {
          this.needRefreshTab();
        }
      });
    } else if (item.link && item.link.type === 'page') {
      if (res.data && res.data.eventResult && item.link.params && item.link.params.eventResult) {
        item.link.params[item.link.params.eventResult] = res.eventResult;
      }
      this.pageModalService.openPage(item.link);
    } else if (res.target) {
      if (res.data && res.data.eventResult && res.params && res.params.eventResult) {
        res.params[res.params.eventResult] = res.data.eventResult;
      }
      const payParams = {
        pageId: res.target,
        params: res.params ? res.params : {},
        sessionId: localStorage.getItem('sessionId')
      };

      this.appGlobalService.goToForward(payParams);
    }
  }
  needRefreshTab() {
    setTimeout(() => {
      const tab = localStorage.getItem('tab');
      if (tab && tab.indexOf("tab") > -1) {
        const tabIndex = tab.substring(tab.length - 1);
        console.log("needRefreshTab" + tabIndex);
        localStorage.setItem('needRefreshTab' + tabIndex, 'true');
        this.eventService.eventEmit.emit(tab, '1');
        console.log('submit eventEmit--->' + tab);
      }
    }, 500);
  }

  getInterger(value) {
    return parseInt(value, 10);
  }


  addAction(submit) {
    this.eventService.eventEmit.emit('addPlatformCaseNow', submit);
  }

  // tslint:disable-next-line:use-life-cycle-interface
  ngOnDestroy(): void {
    this.eventService.removeCustomListListenersByName('hiddenAuditSubmitButton');
  }


}


